package com.labo.erpcrm.controleur;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.labo.erpcrm.metier.ConnexionBusiness;

/**
 * Servlet implementation class ConnextionControleur
 */
@WebServlet("/ConnexionControleur")
public class ConnexionControleur extends HttpServlet {
	private static final long serialVersionUID = 1;
	private static final String VUE_CONNEXION_KO = "/index2.jsp";
	private static final String VUE_CONNEXION_OK = "/accueil.jsp";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ConnexionControleur() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd = null;
		HttpSession session = request.getSession();
		ConnexionBusiness testConnexion = new ConnexionBusiness();
		request.setAttribute("connexion", testConnexion);
		boolean connexion = testConnexion.checkConnexion(request);
		rd = getServletContext().getRequestDispatcher(VUE_CONNEXION_OK);
		if (!connexion) {
			rd = getServletContext().getRequestDispatcher(VUE_CONNEXION_KO);
		}
		else {
			session.setAttribute("current_user", testConnexion.getCurrentUser());
		}
		rd.forward(request, response);
	}

}
